﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoonCode.FocusTest.Utils
{
    class RandomHelper
    {
        private static Random _random = new Random();
        private int min=0, max=10;
        private List<int> cache;
        private string splitChar;
        public RandomHelper(int min,int max,string splitChar="")
        {
            this.min= min;
            this.max= max; 
            this.splitChar = splitChar;
        }

        private void initCache(int ignore)
        {
            for (int i = min; i < max; i++)
            {
                if(i== ignore) continue;
                cache.Add(i);
            }
        }
        public string next(int length,int repeatNum=-1)
        {
            cache = new List<int>();
            string[] result = new string[length];
            int interval = 0;
            int randomInterval = 0;
            for (int i=0;i<length;i++)
            {
                if (randomInterval == 0) randomInterval = _random.Next(3, 7);
                if (repeatNum >= 0&&i>0)
                {
                    if (interval==randomInterval)
                    {
                        result[i] = repeatNum + "";
                        interval = 0;
                        randomInterval = 0;
                        continue;
                    }
                }

                if(cache.Count==0) initCache(repeatNum);
                int num= _random.Next(min, max);
                int index = num % cache.Count;
                result[i] = cache[index]+"";
                cache.RemoveAt(index);
                interval++;
            }
            return string.Join(splitChar,result);
        }
    }
}
